package com.codegenerate.demo.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codegenerate.demo.config.R;
import com.codegenerate.demo.dto.SysRoleDto;
import com.codegenerate.demo.entity.AppFun;
import com.codegenerate.demo.entity.SysRole;
import com.codegenerate.demo.mapper.AppFunMapper;
import com.codegenerate.demo.mapper.SysRoleMapper;
import com.codegenerate.demo.service.SysRoleService;
import com.codegenerate.demo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl <SysRoleMapper, SysRole>
        implements SysRoleService {



    /**
     *  2个表
     *  getWithOne
     getListWithOne
     getPageWithOne
     updateWithOne
     addWithOne
     *
     */

    @Autowired
    AppFunMapper appFunMapper;


    /**
     *  get one with AppFunList
     * @param sysRole
     * @return
     */
    R<SysRole> getSysRoleWithAppFunList(SysRole sysRole){
        var extendQuery=new QueryWrapper<AppFun>().lambda();
        extendQuery.in(AppFun::getId, sysRole.getPermitIds().split(","));
        var extendPage=appFunMapper.selectPage(new Page<AppFun>(1,100),extendQuery);
        var extendList=extendPage.getRecords();
        if(extendList!=null&&extendList.size()>0){
            sysRole.setAppFunList(extendList);
        }
        return R.ok(sysRole);
    }


    /**
     *  getRoleList with AppFuncList
     * @param sysRoleDto
     * @return
     */
    R<Page<SysRole>> getSysRoleListWithAppFunList(SysRoleDto sysRoleDto){
        Page<SysRole> page = this.page(
                sysRoleDto.buildPage(),sysRoleDto.dto2entity().getQueryAll()
        );
        var list=page.getRecords();
        // region 填充extend
        for (int i = 0; i <list.size() ; i++) {
            var appFunResult=getSysRoleWithAppFunList(list.get(i));
            var appFunPage=appFunResult.getData();
            var appFunList = appFunPage.getAppFunList();
            if(appFunList!=null&&appFunList.size()>0){
                list.get(i).setAppFunList( appFunList);
            }
        }
        // endregion
        return R.ok(page);
    }


    /**
     *  modify With AppFunList
     * @param sysRoleDto
     * @return
     */
    R<Long> modifyWithAppFunList(SysRoleDto sysRoleDto){
        var mainId= sysRoleDto.getId();
        var  permitIds=sysRoleDto.getAppFunDtoList().stream().map(item->item.getId())  .collect(Collectors.toList());
        sysRoleDto.setPermitIds(StringUtils.join(",", permitIds));
        if(mainId>0){
            this.getBaseMapper().update(null,  sysRoleDto.dto2entity().getUpdateWrapper());
        }else{
            var entity= sysRoleDto.dto2entity();
            SysRole.setDefaultValue(entity);
            this.save(entity);
            mainId=entity.getId();
        }
        return R.ok(mainId);
    }


}
